/*
 *
 * Copyright (c) 2001-2017 泛微软件.
 * 泛微协同商务系统,版权所有.
 *
 */
package com.weaver.integration.ldap.sync.formart;

import com.alibaba.fastjson.JSONObject;
import com.api.integration.ldap.bean.LdapBaseBean;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.naming.NamingException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;

/**
 * <p>Title: ${file_name}</p>
 * <p>Description: 设置AD密码</p>
 *
 * @author SJZ
 * @version 1.0
 * @date 2017/8/10
 */
public class OA2LdapUserDepartmentFormart implements OaFormart {
    /**
     * 数据转换方法
     *
     * @param baseBean
     * @param data     OA 节点信息
     * @param params   JSON格式的自定义参数信息
     * @return
     */
    @Override
    public String formart(LdapBaseBean baseBean, Map<String, String> data, String params) {
//        new BaseBean().writeLog("OA转换部门时:"+baseBean.getLdapId());
//        StringBuilder sb = new StringBuilder();
//        Iterator<Map.Entry<String, String>> iter = data.entrySet().iterator();
//        while (iter.hasNext()) {
//            Map.Entry<String, String> entry = iter.next();
//            sb.append(entry.getKey());
//            sb.append('=').append('"');
//            sb.append(entry.getValue());
//            sb.append('"');
//            if (iter.hasNext()) {
//                sb.append(',').append(' ');
//            }
//        }
//        new BaseBean().writeLog("AD部门转换map的值为:"+sb.toString());



//        JSONObject jsonObject = JSONObject.parseObject(params);
//        new BaseBean().writeLog("AD同步,部门转换获取到的参数:"+jsonObject.toString());
        //数据库操作
        RecordSet rs = new RecordSet();
        //返回部门名称
        String strDepartmentName = "";

        String strUserid = data.get("id");
        new BaseBean().writeLog("AD同步部门转换人员id:"+strUserid);
        //根据人员得到其部门名称
        String strSql = "select hd.departmentname from hrmresource hr ,hrmdepartment hd where hr.departmentid =hd.id and hr.id=? ";
        rs.executeQuery(strSql, strUserid);
        if (rs.next()) {
            strDepartmentName = Util.null2String(rs.getString("departmentname"));
        }
        new BaseBean().writeLog("AD同步部门转换返回部门名称:"+strDepartmentName);


        return strDepartmentName;
    }

    /**
     * 获取转换结果 转换失败后会记录该数据待数据同步完成后再次处理
     *
     * @return
     */
    @Override
    public boolean getStatus() {
        return true;
    }
}
